<?php
/**
 * 升级程序5.1.2 to 5.2.0
 * @copyright (c) Emlog All Rights Reserved
 */
require_once('init.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>emlog升级程序</title>
<style type="text/css">
<!--
body {
    background-color:#F7F7F7;
	font-family: Arial;
	font-size: 12px;
	line-height:150%;}
.main {
	background-color:#FFFFFF;
	margin-top:20px;
	font-size: 12px;
	color: #666666;
	width:580px;
	margin:10px auto;
	padding:10px;
	list-style:none;
	border:#DFDFDF 1px solid;}
.input {
	border: 1px solid #CCCCCC;
	font-family: Arial;
	font-size: 18px;
	height:28px;
	background-color:#F7F7F7;
	color: #666666;
	margin:5px 25px;}
.submit{
	background-color:#FFFFFF;
	border: 3px double #999;
	border-left-color: #ccc;
	border-top-color: #ccc;
	color: #333;
	padding: 0.25em;
	cursor:hand;}
.title{font-size:20px;}
.care{color:#0066CC;padding:0px 10px;}
.title2{font-size:14px;color:#000000;border-bottom: #CCCCCC 1px solid;}
.foot{text-align:center;}
li{border-bottom:#CCCCCC 1px dotted;margin:20px 20px;}
-->
</style>
<?php
if(!isset($_GET['action'])){
?>
<form name="form1" method="post" action="up5.1.2to5.2.0.php?action=install">
<div class="main">
<div>
<p><span class="title">emlog <span style="color: #0099FF">5.1.2</span> -----&raquo; <span style="color: #FF0000; font-size:26px">5.2.0</span></span><span> 升级程序</span></p>
<p>详细升级步骤见升级程序包内的：升级说明.html</p>
</div>
<div class="b">
<p class="title2"></p>
<li>
    <strong>数据库用户密码：</strong><span class="care">(服务器上config.php文件里 DB_PASSWD 对应值)</span><br />
  <input name="password" type="password" class="input" value="">
</li>
</div>
<div>
<p class="foot">
<input type="submit" class="submit" value="确 定">
<input type="reset" class="submit" value="重 置">
</p>
</div>
<p class="foot">
&copy;2012 emlog
</p>
</div>
</div>
</form>
<?php
}

if (isset($_GET['action'])&&$_GET['action'] == "install") {
	$DB = MySql::getInstance();
    $db_prefix = DB_PREFIX;

	$dbcharset = 'utf8';
	$type = 'MYISAM';
	$extra = "ENGINE=".$type." DEFAULT CHARSET=".$dbcharset.";";
	$extra2 = "TYPE=".$type;
	$add = $DB->getMysqlVersion() > '4.1' ? $extra : $extra2.";";

	$widgets = Option::getWidgetTitle();
    $sider_wg = Option::getDefWidget();

	$widget_title = serialize($widgets);
	$widgets = serialize($sider_wg);

	if (DB_PASSWD != trim($_POST['password'])){
	    emMsg("输入的数据库密码错误,请重新输入");
	}

	if (Option::EMLOG_VERSION != '5.2.0') {
		emMsg("错误操作：您必须完成升级步骤里的第一步才再进行本操作，详见安装说明");
	}
	
    if ($DB->num_rows($DB->query("SELECT * FROM {$db_prefix}options WHERE option_name='isexcerpt'")) == 1) {
        emMsg("数据库已升级完成，请勿重复运行升级脚本");
    }

$sql = "
ALTER TABLE {$db_prefix}blog DROP COLUMN tbcount;
ALTER TABLE {$db_prefix}blog DROP COLUMN allow_tb;
ALTER TABLE {$db_prefix}blog ADD COLUMN checked enum('n','y') NOT NULL default 'y' AFTER hide;
INSERT INTO {$db_prefix}options (option_name, option_value) VALUES ('isexcerpt','n');
INSERT INTO {$db_prefix}options (option_name, option_value) VALUES ('excerpt_subnum','300');
DROP TABLE IF EXISTS {$db_prefix}trackback;
DELETE FROM {$db_prefix}options WHERE option_name='istrackback';
ALTER TABLE {$db_prefix}user ADD COLUMN ischeck enum('n','y') NOT NULL default 'n' AFTER role;
";

	$array_sql = preg_split("/;[\r\n]/", $sql);
	foreach($array_sql as $sql)
	{
		$sql = trim($sql);
		if ($sql) {
			$DB->query($sql);
		}
	}

	@unlink('./content/cache/comment');
	@unlink('./content/cache/link');
	@unlink('./content/cache/logalias');
	@unlink('./content/cache/logatts');
	@unlink('./content/cache/logsort');
	@unlink('./content/cache/logtags');
	@unlink('./content/cache/newlog');
	@unlink('./content/cache/newtw');
	@unlink('./content/cache/options');
	@unlink('./content/cache/record');
	@unlink('./content/cache/sort');
	@unlink('./content/cache/sta');
	@unlink('./content/cache/tags');
	@unlink('./content/cache/user');

	$CACHE->updateCache();

	@unlink('./install.php');
	@unlink('./up5.1.2to5.2.0.php');
	@unlink('./tb.php');
	emMsg("恭喜你！emlog已成功升级到5.2.0 ,请清空浏览器缓存，<a href=\"./\"> 进入博客&raquo; </a>");
}
echo "</body>";
echo "</html>";
?>